package cc.autoapi.pucong.liteflow.design.core.configuration;

import cc.autoapi.pucong.liteflow.design.service.DesignService;
import org.apache.commons.lang3.StringUtils;

import java.util.Optional;

public class LiteFlowDesignConfiguration {

    // 数据库处理
    private DesignService designService;
    /**
     * 只读模式
     */
    private Boolean readonly = false;

    /**
     * 登录有效时间 分钟
     */
    private Long loginExpire = 43200L;

    /**
     * 是否装配liteflow design
     */
    private Boolean enable = true;

    /**
     * web页面入口
     */
    private String web = "/liteFlow-design-web";

    // 是否需要登录
    private Boolean requireLogin;

    // 登录验证
    private AuthorizationInterceptor authorizationInterceptor;
    /**
     * 是否启用跨域支持
     *
     * @since 1.0.0
     */
    private boolean supportCrossDomain = true;

    public Boolean getReadonly() {
        return readonly;
    }

    public void setReadonly(Boolean readonly) {
        this.readonly = readonly;
    }

    public boolean isSupportCrossDomain() {
        return supportCrossDomain;
    }

    public void setSupportCrossDomain(boolean supportCrossDomain) {
        this.supportCrossDomain = supportCrossDomain;
    }

    public String getWeb() {
        return web;
    }

    public String getWebPath() {
        if (StringUtils.isBlank(web)) {
            return null;
        }
        if (web.endsWith("/**")) {
            return web.substring(0, web.length() - 3);
        }
        if (web.endsWith("/*")) {
            return web.substring(0, web.length() - 2);
        }
        if (web.endsWith("/")) {
            return web.substring(0, web.length() - 1);
        }
        return web;
    }

    public void setWeb(String web) {
        this.web = web;
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public DesignService getDesignService() {
        return designService;
    }

    public void setDesignService(DesignService designService) {
        this.designService = designService;
    }

    public AuthorizationInterceptor getAuthorizationInterceptor() {
        return authorizationInterceptor;
    }

    public void setAuthorizationInterceptor(AuthorizationInterceptor authorizationInterceptor) {
        this.authorizationInterceptor = authorizationInterceptor;
    }

    public Boolean getRequireLogin() {
        return requireLogin;
    }

    public void setRequireLogin(Boolean requireLogin) {
        this.requireLogin = requireLogin;
    }

    public Long getLoginExpire() {
        return loginExpire;
    }

    public void setLoginExpire(Long loginExpire) {
        this.loginExpire = loginExpire;
    }

    /**
     * 打印banner
     */
    public void printBanner(String systemPath, Integer systemPort) {
        System.out.println(".__  .__  __          _____.__                              .___            .__               ");
        System.out.println("|  | |__|/  |_  _____/ ____\\  |   ______  _  __           __| _/____   _____|__| ____   ____  ");
        System.out.println("|  | |  \\   __\\/ __ \\   __\\|  |  /  _ \\ \\/ \\/ /  ______  / __ |/ __ \\ /  ___/  |/ ___\\ /    \\ ");
        System.out.println("|  |_|  ||  | \\  ___/|  |  |  |_(  <_> )     /  /_____/ / /_/ \\  ___/ \\___ \\|  / /_/  >   |  ");
        System.out.println("|____/__||__|  \\___  >__|  |____/\\____/ \\/\\_/           \\____ |\\___  >____  >__\\___  /|___|  /");
        System.out.println("                   \\/                                        \\/    \\/     \\/  /_____/      \\/ " + getVersion() + "  " + getUrl(systemPath, systemPort));
    }

    private String getUrl(String systemPath, Integer systemPort) {
        return String.format("http://127.0.0.1:%d%s/liteFlow-design-web/index.html", systemPort, systemPath);
    }

    private String getVersion() {
        return Optional.ofNullable(LiteFlowDesignConfiguration.class.getPackage()).map(Package::getImplementationVersion).orElse("DEV");
    }
}
